package net.citrite.android;


import com.android.SdkConstants;
import com.android.sdklib.IAndroidTarget;
import com.android.sdklib.devices.Device;
import com.android.sdklib.devices.DeviceManager;
import com.android.sdklib.repository.local.LocalSdk;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;

public class PreviewManager {
    private final static Logger logger = Logger.getLogger(PreviewManager.class.getName());
    private LocalSdk localSdk = new LocalSdk();

    public PreviewManager() throws URISyntaxException {
        Properties properties = System.getProperties();
        String pathSeparator = properties.getProperty("path.separator");
        String[] list = properties.getProperty("java.ext.dirs").split(pathSeparator);
        for (String ff : list) {
            File folder = new File(ff);
            File propertyFile = new File(folder.getParent(), SdkConstants.FN_SOURCE_PROP);
            if (propertyFile.isFile()) {
                localSdk.setLocation(propertyFile.getParentFile());
                break;
            }
        }
        if (localSdk.getLocation() == null) {
            String sdkLocation = System.getProperty("sdk");
            if (sdkLocation != null && !sdkLocation.isEmpty()) {
                File sdkDirectory = new File(sdkLocation);
                if (sdkDirectory.isDirectory())
                    localSdk.setLocation(sdkDirectory);
            }
        }
//        if (localSdk.getLocation() == null) {
//            URL loc = PreviewManager.class.getClassLoader().getResource("");
//            File folder = new File(loc.toURI());
//
//            File propertyFile = new File(folder.getParent(), SdkConstants.FN_SOURCE_PROP);
//            if (propertyFile.isFile()) {
//                localSdk.setLocation(propertyFile.getParentFile());
//            }
//        }

        if (localSdk.getLocation() == null) {
            throw new IllegalArgumentException("sdk not found");
        }
    }

    public void setAndroidSdk(String sdk) {
        File sdkLocation = new File(sdk);
        setAndroidSdk(sdkLocation);
    }

    public void setAndroidSdk(File sdkLocation) {
        localSdk.setLocation(sdkLocation);
    }

    public IAndroidTarget getDefaultTarget() {
        IAndroidTarget[] targets = localSdk.getTargets();
        for (int i = targets.length - 1; i >= 0; i--) {
            if (targets[i].isPlatform() && targets[i].hasRenderingLibrary()) {
                return targets[i];
            }
        }
        return null;
    }

    public IAndroidTarget getTargetById(int id) {
        IAndroidTarget[] targets = localSdk.getTargets();
        for (int i = targets.length - 1; i >= 0; i--) {
            IAndroidTarget target = targets[i];
            if (target.isPlatform() && target.hasRenderingLibrary() && target.getName().equals(id)) {
                return targets[i];
            }
        }
        return null;
    }

    public Device getDefaultDevice() {
        return getDeviceById("Nexus 4");
    }

    public Device getDeviceById(String id) {
        LogWrapper logWrapper = new LogWrapper(logger);
        DeviceManager deviceManager = DeviceManager.createInstance(localSdk.getLocation(), logWrapper);
        List<Device> devices = new ArrayList<Device>();
        devices.addAll(deviceManager.getDevices(EnumSet.of(DeviceManager.DeviceFilter.DEFAULT, DeviceManager.DeviceFilter.VENDOR)));
        for (Device device : devices) {
            if (device.getId().equals(id)) {
                return device;
            }
        }
        return null;
    }
}
